// #[macro_use]
// extern crate lazy_static;

// use std::sync::Mutex;

slint::include_modules!();

pub mod common;
use std::{
    borrow::BorrowMut,
    cell::{OnceCell, RefCell},
    rc::{Rc, Weak},
};

use common::CsrMatrix;

use crate::common::common::A;

// static mut A : common::CsrMatrix = common::CsrMatrix {
//     col_index: Vec::new(),
//     row_ptr: Vec::new(),
//     value: Vec::new(),
//     num_rows: 0,
//     num_cols: 0,
//     nnz: 0,
// };

const NUMBERXY: i32 = 10;

fn main() -> Result<(), slint::PlatformError> {
    //let A = Rc::new(RefCell::new(CsrMatrix::new()));
    //let B = OnceCell::new();
    let ui = AppWindow::new()?;

    //ui.global()

    ui.on_from_file({
        let ui_handle = ui.as_weak();
        move || {
            let ui = ui_handle.unwrap();
            print!("点击了从文件读取\n");
        }
    });
    ui.on_from_rand({
        let ui_handle = ui.as_weak();
        move || {
            let ui = ui_handle.unwrap();
            print!("点击了随机生成\n");
        }
    });

    ui.on_edit_file_path({
        let ui_handle = ui.as_weak();
        move |file_path| {
            let ui = ui_handle.unwrap();
            print!("点击了输入文件路径\n");
            println!("the file path is: { }", file_path.as_str());
        }
    });

    ui.on_init_matrix({
        let ui_handle = ui.as_weak();
        //let A_mut = A.clone();
        //let A_m = A_mut.borrow_mut();
        move || {
            let ui = ui_handle.unwrap();
            print!("点击了初始化矩阵\n");
            if ui.get_is_from_file() {
                //let ta = CsrMatrix::new();
                unsafe {
                    A.create_host_data(ui.get_file_path().as_str());
                    ui.set_rows(A.num_rows as i32);
                    ui.set_nnz(A.nnz as i32);
                }
                ui.set_number_xy(NUMBERXY);
            } else {
            }
        }
    });
    ui.on_select_matrix_format({
        let ui_handle = ui.as_weak();
        move |matrix_format| {
            let ui = ui_handle.unwrap();
            print!("点击了选择矩阵格式\n");
            println!("the matrix format is: { }", matrix_format.as_str());
        }
    });
    ui.on_edit_rows({
        let ui_handle = ui.as_weak();
        move |rows| {
            let ui = ui_handle.unwrap();
            print!("点击了输入行数\n");
            let Rows: i32 = rows.as_str().parse().unwrap();
            println!("the rows is: { }", Rows);
        }
    });
    ui.on_edit_nnz({
        let ui_handle = ui.as_weak();
        move |nnz| {
            let ui = ui_handle.unwrap();
            print!("点击了输入非零元素个数\n");
            let Nnz: i32 = nnz.as_str().parse().unwrap();
            println!("the nnz is: { }", Nnz);
        }
    });
    ui.on_edit_numberxy({
        let ui_handle = ui.as_weak();
        move |numberxy| {
            let ui = ui_handle.unwrap();
            print!("点击了输入矩阵大小\n");
            let Numberxy: i32 = numberxy.as_str().parse().unwrap();
            println!("the numberxy is: { }", Numberxy);
        }
    });
    ui.on_cpu_calc({
        let ui_handle = ui.as_weak();
        move || {
            let ui = ui_handle.unwrap();
            print!("点击了CPU计算\n");
        }
    });
    ui.on_gpu_calc({
        let ui_handle = ui.as_weak();
        move || {
            let ui = ui_handle.unwrap();
            print!("点击了GPU计算\n");
        }
    });
    ui.on_start_calc({
        let ui_handle = ui.as_weak();
        //let weak = Rc::downgrade(&A);
        move || {
            //let ui = ui_handle.unwrap();
            print!("点击了开始计算\n");
            unsafe {
                //calc(&*weak.as_ptr());
                A.calc();
            }
        }
    });

    ui.invoke_gpu_calc();

    // {
    //     print!("点击了开始计算\n");
    //     calc(&A);
    // };
    ui.run()
}
